package com.cate.action;

  
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.cate.bean.User;
import com.cate.service.UserService;
import com.cate.util.PasswordHelper;


  
@Controller  
public class LoginAction {  
	@Autowired
	UserService userService;
	@Autowired
	PasswordHelper psw;
	
//	@RequestMapping("dologin")  
//	public String doLogin(HttpServletRequest request, Model model) {  
//	    String msg = "";  
//	    String userName = request.getParameter("userName");  
//	    String password = request.getParameter("password");  
//	    System.out.println(userName);  
//	    System.out.println(password);  
//	    UsernamePasswordToken token = new UsernamePasswordToken(userName, password);  
//	    token.setRememberMe(true);  
//	    Subject subject = SecurityUtils.getSubject();  
//	    try {  
//	        subject.login(token);  
//	        if (subject.isAuthenticated()) {  
//	            return "redirect:/";  
//	        } else {  
//	            return "login";  
//	        }  
//	    } catch (IncorrectCredentialsException e) {  
//	        msg = "登录密码错误. Password for account " + token.getPrincipal() + " was incorrect.";  
//	        model.addAttribute("message", msg);  
//	        System.out.println(msg);  
//	    } catch (ExcessiveAttemptsException e) {  
//	        msg = "登录失败次数过多";  
//	        model.addAttribute("message", msg);  
//	        System.out.println(msg);  
//	    } catch (LockedAccountException e) {  
//	        msg = "帐号已被锁定. The account for username " + token.getPrincipal() + " was locked.";  
//	        model.addAttribute("message", msg);  
//	        System.out.println(msg);  
//	    } catch (DisabledAccountException e) {  
//	        msg = "帐号已被禁用. The account for username " + token.getPrincipal() + " was disabled.";  
//	        model.addAttribute("message", msg);  
//	        System.out.println(msg);  
//	    } catch (ExpiredCredentialsException e) {  
//	        msg = "帐号已过期. the account for username " + token.getPrincipal() + "  was expired.";  
//	        model.addAttribute("message", msg);  
//	        System.out.println(msg);  
//	    } catch (UnknownAccountException e) {  
//	        msg = "帐号不存在. There is no user with username of " + token.getPrincipal();  
//	        model.addAttribute("message", msg);  
//	        System.out.println(msg);  
//	    } catch (UnauthorizedException e) {  
//	        msg = "您没有得到相应的授权！" + e.getMessage();  
//	        model.addAttribute("message", msg);  
//	        System.out.println(msg);  
//	    }  
//	    return "login";  
//	} 
	
	@RequestMapping("show")
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mode = new ModelAndView("main_bg");
		return mode;
	}
	
	@RequestMapping("gologin")
	public ModelAndView gologin(HttpServletRequest request, HttpServletResponse response) {
		Subject currentUser = SecurityUtils.getSubject();  
		System.out.println("==============");
		ModelAndView mode = new ModelAndView();
		if(currentUser.isAuthenticated()){
			mode.setViewName("main_bg");
		}else{
			mode.setViewName("login_bg");
		}
		return mode;
	}
	
	@RequestMapping("add")
	public ModelAndView add(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mode = new ModelAndView("add");
		return mode;
	}
	
	@RequestMapping("save")
	public String save(User user ,HttpServletRequest request, HttpServletResponse response) {
		HashMap<String,Object> map = new HashMap<String,Object>();
		user = psw.encryptPassword(user);
		map.put("id", "id");
		map.put("table_name", "test_user");
		Long id = userService.getNextSeq(map);
		user.setId(id);
		String url = null;
		try {
			userService.addUser(user);
			request.setAttribute("msg", "注册成功！");
			url = "addSuccess";
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("msg", e.getMessage());
			url = "/page/add.jsp";
		}
		return "addSuccess";
	}
}  